﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using CtpApi;
using CtpApi.Struct;
using TraderClient.Logic.CTP;

namespace TraderClient.Views.Trade
{
    public partial class ManualTradeForm : Form
    {
        private MdApi mdApi;
        private TradeApi tdApi;

        private System.Windows.Forms.Timer timer;

        public ManualTradeForm()
        {
            InitializeComponent();
        }

        private void ManualTraderForm_Load(object sender, EventArgs e)
        {
            string[] frontAddresses = {@"tcp://asp-sim2-front1.financial-trading-platform.com:26205"};

            mdApi = new MdApi();
            tdApi = new TradeApi("4030", "80002863", "888888", frontAddresses);

            tdApi.RtnOrder += OnRtnOrder;
            tdApi.RspQryTradingAccount += OnRspQryTradingAccount;
            tdApi.RtnInstrumentStatus += OnRtnInstrumentStatus;
            tdApi.RspQryInvestorPosition += OnRspQryInvestorPosition;
            tdApi.RspQryInvestorPositionDetail += OnRspQryInvestorPositionDetail;
            tdApi.RspQryInvestorPositionCombineDetail += OnRspQryInvestorPositionCombineDetail;

            timer = new System.Windows.Forms.Timer();

            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = 500;
            timer.Start();
        }


        /// <summary>
        /// 定时获取行情数据并显示
        /// </summary>
        private void timer_Tick(object sender, EventArgs e)
        {
            CThostFtdcDepthMarketDataField depthMarketDataField = mdApi.GetDepthMarketData();

            if (depthMarketDataField != null)
            {
                labelPrice.Text = depthMarketDataField.LastPrice.ToString();
            }
        }

        private List<CThostFtdcOrderField> orders = new List<CThostFtdcOrderField>();

        //报单响应
        void OnRtnOrder(CThostFtdcOrderField pOrder)
        {
            if (pOrder == null) return;

            orders.Add(pOrder);

            Action ac = () =>
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = orders;
                dataGridView2.DataSource = bs;
            };

            this.Invoke(ac);
        }

        //查询资金账户响应
        private void OnRspQryTradingAccount(CThostFtdcTradingAccountField pTradingAccount, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {
            if (pTradingAccount == null) return;

            Action<CThostFtdcTradingAccountField> ac = (account) => {
                listBoxAccount.Items.Clear();
                listBoxAccount.Items.Add("可用资金      " + account.Available.ToString());
                listBoxAccount.Items.Add("保证金额      " + account.CurrMargin.ToString());
                listBoxAccount.Items.Add("持仓盈亏      " + account.PositionProfit.ToString());
            };

            this.Invoke(ac, pTradingAccount);
        }

        private List<CThostFtdcInvestorPositionField> positions = new List<CThostFtdcInvestorPositionField>();

        //查询投资者持仓响应
        private void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField pInvestorPosition, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {
            if (pInvestorPosition == null) return;

            positions.Add(pInvestorPosition);

            if (bIsLast)
            {
                Action ac = () =>
                {
                    BindingSource bs = new BindingSource();
                    bs.DataSource = positions;
                    dataGridView1.DataSource = bs;
                };

                this.Invoke(ac);
            }
        }

        private List<CThostFtdcInvestorPositionDetailField> positionDetail = new List<CThostFtdcInvestorPositionDetailField>();

        //查询投资者持仓明细响应
        void OnRspQryInvestorPositionDetail(CThostFtdcInvestorPositionDetailField pInvestorPositionDetail, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {
            if (pInvestorPositionDetail == null) return;

            positionDetail.Add(pInvestorPositionDetail);

            if (bIsLast)
            {
                Action ac = () =>
                {
                    BindingSource bs = new BindingSource();
                    bs.DataSource = positionDetail;
                    dataGridView1.DataSource = bs;
                };

                this.Invoke(ac);
            }
        }

        //查询投资者组合持仓明细响应
        void OnRspQryInvestorPositionCombineDetail(CThostFtdcInvestorPositionCombineDetailField pInvestorPositionCombineDetail, CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
        {

        }

        //合约交易状态通知
        void OnRtnInstrumentStatus(CThostFtdcInstrumentStatusField pInstrumentStatus)
        {
            if (pInstrumentStatus != null)
            {
                Action<CThostFtdcInstrumentStatusField> ac = (instrumentStatus) => {
                    label2.Text = instrumentStatus.InstrumentID + "|" + instrumentStatus.InstrumentStatus.ToString();
                };

                this.Invoke(ac, pInstrumentStatus);
            }
        }


        private void btnBuy_Click(object sender, EventArgs e)
        {
            string instrumentID = "ru1205";
            double price = double.Parse(textBox1.Text);
            int volume = 1;

            tdApi.Buy(instrumentID, price, volume);
        }

        private void btnSell_Click(object sender, EventArgs e)
        {
            string instrumentID = "ru1205";
            double price = double.Parse(textBox1.Text);
            int volume = 1;

            tdApi.Sell(instrumentID, price, volume);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            string instrumentID = "ru1205";
            double price = double.Parse(textBox1.Text);
            int volume = 1;

            //tdApi.Close(instrumentID, price, volume);
        }

        private void btnQryAccount_Click(object sender, EventArgs e)
        {
            tdApi.QryTradingAccount();
        }

        private void btnQryPosition_Click(object sender, EventArgs e)
        {
            tdApi.QryInvestorPositionDetail();
        }

        private void btnInstrument_Click(object sender, EventArgs e)
        {
            tdApi.QryInstrument("ru1205");
        }




    }
}
